package com.pptiku.jh.routerdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.pptiku.jh.routerdemo.dagger2.bean.Pot;
import com.pptiku.jh.routerdemo.dagger2.components.DaggerFlowerComponent;
import com.pptiku.jh.routerdemo.dagger2.components.DaggerMainActivityComponent;
import com.pptiku.jh.routerdemo.dagger2.components.DaggerPotComponent;

import javax.inject.Inject;

public class MainActivity extends AppCompatActivity
{

//    @Inject
//    @MUser
//    User mm;
//
//    @Inject
//    @SUser
//    String ns;

    @Inject
    Pot pot;

    protected LoadService mLoadService;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLoadService = LoadSir.getDefault()
                .register(registerTarget(),
                        new Callback.OnReloadListener()
                        {
                            @Override
                            public void onReload(View v)
                            {
                                onPageRetry(v);
                            }
                        });
        mLoadService.showSuccess();
//        DaggerActivityComponent.builder()
//                .appComponent(App.getApp().getAppComponent())
//                .build()
//                .inject(this);

        DaggerMainActivityComponent.builder()
                .potComponent(DaggerPotComponent.builder().flowerComponent(DaggerFlowerComponent.create()).build())
                .build()
                .inject(this);
        Toast.makeText(this, pot.show(), Toast.LENGTH_SHORT).show();
    }

//    new Convertor<HttpResult>() {
//    @Override
//    public Class<? extends Callback> map(HttpResult httpResult) {
//        Class<? extends Callback> resultCode = SuccessCallback.class;
//        switch (httpResult.getResultCode()) {
//            case SUCCESS_CODE:
//                if (httpResult.getData().size() == 0) {
//                    resultCode = EmptyCallback.class;
//                }else{
//                    resultCode = SuccessCallback.class;
//                }
//                break;
//            case ERROR_CODE:
//                resultCode = ErrorCallback.class;
//                break;
//        }
//        return resultCode;
//    }
//}

    private void onPageRetry(View v)
    {
        Toast.makeText(this, "重试", Toast.LENGTH_SHORT).show();
    }


    /**
     * loadsir注册目标，默认为自身acitivity
     */
    protected Object registerTarget()
    {
        return this;
    }

    public void click(View view)
    {
//        Toast.makeText(this, ns, Toast.LENGTH_SHORT).show();
//        mLoadService.showCallback(RetryCallback.class);
        ARouter.getInstance().build("/business/web")
                .withString("url", "file:///android_asset/a.html")
                .greenChannel()
                .navigation();
    }
}
